package com.qingcheng.controller.file;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("/upload")
public class UploadController {

    @Autowired
    private HttpServletRequest request;

    @PostMapping("/native")//post方式提交数据
    public String nativeUpload(@RequestParam("file") MultipartFile file)//@RequestParam("file")指定参数名为file
    {
        System.out.println("图片你倒是上传啊");
            String path=request.getSession().getServletContext().getRealPath("img");
            String filePath=path+"/"+file.getOriginalFilename();

            File desFile=new File(filePath);
            if(!desFile.getParentFile().exists())
            {
                desFile.mkdirs();
            }

        try {
            file.transferTo(desFile);//desFile目标文件
        } catch (IOException e) {
            e.printStackTrace();
        }
            return "http://localhost:9101/img/"+file.getOriginalFilename();

    }

}
